<template>
  <div>
    <div style="text-align: left;margin-left:2em">
      <el-button type="text" @click="addProjectDate" style="margin-right:20px" size="medium" :disabled="disableEditSubmit || disableSave" v-show="!(disableEditSubmit || disableSave)"><i class="el-icon-circle-plus-outline"></i>新增挂网开标日期</el-button>
    </div>
    <div class="tl-form-line-title" v-if="model.projectDateList && model.projectDateList.length > 0 && forview">
      挂网开标日期
    </div>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
      <el-row v-for="(item, index) in model.projectDateList" :key="index">
        <el-col :span="8">
          <el-button type="text" @click="deleteProjectDate(index)" size="medium" v-show="!(disableEditSubmit || disableSave)" style="float: left;padding-top: 6px"><i class="el-icon-remove-outline"></i>删除</el-button>
          <el-form-item label="招标批次" :prop="'projectDateList.' + index + '.batchnumber'" label-width="100px" style="margin-left: 50px" :rules="biddatetime ? rules.validateRequire : []">
            <el-input v-model="item.batchnumber"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="挂网日期" :prop="'projectDateList.' + index + '.projectbidbegindate'" :rules="biddatetime ? rules.validateRequire : []">
            <el-date-picker v-model="item.projectbidbegindate" type="date" placeholder="挂网日期" value-format="yyyy-MM-dd" @change="bidbegindate(index)"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开标日期" :prop="'projectDateList.' + index + '.projectbidenddate'" :rules="biddatetime ? rules.validateRequire : []">
            <el-date-picker v-model="item.projectbidenddate" type="date" placeholder="开标日期" value-format="yyyy-MM-dd" @change="bidenddate(index)"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { validateNull } from '@tapui/utils/validate'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: {},
  data() {
    var validateForTab = (rule, value, callback) => {
      if (!value) {
        this.tabName = rule.tabname
        return callback(new Error('请输入内容'))
      } else {
        return callback()
      }
    }
    return {
      moduleTitle: '项目立项-挂网开标日期',
      urlPrefix: '/mpm/projectBidDate/',
      url: {
        // 额外的url
        loadInitDateInfo: '/mpm/projectBidDate/loadDateInfo',
        loadDateInfo: '/mpm/projectBidDateHistory/loadDateInfo'
      },
      addRules: {},
      disableSave: true
    }
  },
  props: {
    projectid: {
      type: Number,
      default: 0
    },
    planstartdate: {
      type: String,
      default: ''
    },
    planenddate: {
      type: String,
      default: ''
    },
    biddatetime: {
      type: Boolean,
      default: false
    },
    forview: {
      type: Boolean,
      default: false
    },
    projectIdForBidDate: {
      type: Number,
      default: 0
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
    open() {
      this.$set(this.model, 'projectDateList', [{}])
      let that = this
      that.$nextTick(() => {
        that.$refs.form.clearValidate()
        if (that.isAdd === true) {
          that.queryInitData(that.projectIdForBidDate).then((_) => {
            that.loaded()
          })
        } else if (that.disableEditSubmit === false) {
          that.title = '编辑' + that.moduleTitle
          that.queryData(that.projectid).then((_) => {
            that.loaded()
          })
        } else {
          that.title = '查看' + that.moduleTitle
          that.queryData(that.projectid).then((_) => {
            that.$refs.form.clearValidate()
            that.loaded()
          })
        }
      })
    },
    queryData(projectid) {
      if (!this.url.loadDateInfo) {
        this.$tap.alert('请设置loadDateInfo地址!', 'warning')
        return
      }
      return getAction(this.url.loadDateInfo, { projectid: projectid }).then((res) => {
        if (res.success) {
          // 不显示挂网开标日期为空的记录
          let biddate = res.result.filter((item) => {
            if (!item.projectbidbegindate && !item.projectbidenddate) {
              return false
            } else {
              return true
            }
          })
          this.$set(this.model, 'projectDateList', biddate)
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    queryInitData(projectid) {
      if (!this.url.loadInitDateInfo) {
        this.$tap.alert('请设置loadInitDateInfo地址!', 'warning')
        return
      }
      return getAction(this.url.loadInitDateInfo, { projectid: projectid }).then((res) => {
        if (res.success) {
          // 不显示挂网开标日期为空的记录
          let biddate = res.result.filter((item) => {
            if (!item.projectbidbegindate && !item.projectbidenddate) {
              return false
            } else {
              return true
            }
          })
          let biddateArray = []
          biddate.forEach((item) => {
            biddateArray.push({ batchnumber: item.batchnumber, projectbidbegindate: item.projectbidbegindate, projectbidenddate: item.projectbidenddate })
          })
          this.$set(this.model, 'projectDateList', biddateArray)
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    // 添加挂网招标日期
    addProjectDate() {
      this.model.projectDateList.push({
        projectid: this.projectid
      })
    },
    // 删除挂网招标日期
    deleteProjectDate(index) {
      if (this.biddatetime && this.model.projectDateList.length === 1) {
        this.$tap.alert('挂网招标日期数量为1时，不允许删除')
      } else {
        this.model.projectDateList.splice(index, 1)
      }
    },
    // 校验挂网日期
    bidbegindate(index) {
      if (this.planstartdate === '' || typeof this.planstartdate === 'undefined' || this.planenddate === '' || typeof this.planenddate === 'undefined') {
        this.$tap.alert('请先选择计划开始日期和计划结束日期！', 'warning')
        this.$set(this.model.projectDateList[index], 'projectbidbegindate', '')
        return
      }
      var planenddate = new Date(this.planenddate).getTime()
      var planstartdate = new Date(this.planstartdate).getTime()
      var bidbegindate = new Date(this.model.projectDateList[index].projectbidbegindate).getTime()
      if (bidbegindate < planstartdate) {
        this.$tap.alert('挂网日期不能早于计划开始日期！', 'warning')
        this.$set(this.model.projectDateList[index], 'projectbidbegindate', '')
        return
      }
      if (bidbegindate > planenddate) {
        this.$tap.alert('挂网日期不能晚于计划结束日期！', 'warning')
        this.$set(this.model.projectDateList[index], 'projectbidbegindate', '')
        return
      }
      if (this.model.projectDateList[index].projectbidenddate) {
        var bidenddate = new Date(this.model.projectDateList[index].projectbidenddate).getTime()
        if (bidbegindate > bidenddate) {
          this.$tap.alert('挂网日期不能晚于开标日期！', 'warning')
          this.$set(this.model.projectDateList[index], 'projectbidbegindate', '')
          return
        }
      }
    },
    // 校验开标日期
    bidenddate(index) {
      if (this.planstartdate === '' || typeof this.planstartdate === 'undefined' || this.planenddate === '' || typeof this.planenddate === 'undefined') {
        this.$tap.alert('请先选择计划开始日期和计划结束日期！', 'warning')
        this.$set(this.model.projectDateList[index], 'projectbidenddate', '')
        return
      }
      var planenddate = new Date(this.planenddate).getTime()
      var planstartdate = new Date(this.planstartdate).getTime()
      var bidenddate = new Date(this.model.projectDateList[index].projectbidenddate).getTime()
      if (bidenddate < planstartdate) {
        this.$tap.alert('开标日期不能早于计划开始日期！', 'warning')
        this.$set(this.model.projectDateList[index], 'projectbidenddate', '')
        return
      }
      if (bidenddate > planenddate) {
        this.$tap.alert('开标日期不能晚于计划结束日期！', 'warning')
        this.$set(this.model.projectDateList[index], 'projectbidenddate', '')
        return
      }
      if (this.model.projectDateList[index].projectbidbegindate) {
        var bidbegindate = new Date(this.model.projectDateList[index].projectbidbegindate).getTime()
        if (bidbegindate > bidenddate) {
          this.$tap.alert('挂网日期不能晚于开标日期！', 'warning')
          this.$set(this.model.projectDateList[index], 'projectbidenddate', '')
          return
        }
      }
    },
    validateBidDate() {
      let that = this
      if (that.biddatetime) {
        if (that.model.projectDateList.length === 0) {
          return false
        }
        for (let i = 0; i < that.model.projectDateList.length; i++) {
          let bid = that.model.projectDateList[i]
          if (validateNull(bid.batchnumber) || validateNull(bid.projectbidbegindate) || validateNull(bid.projectbidenddate)) {
            return false
          }
        }
      }
      return true
    }
  }
}
</script>
